﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace JQuery.Net.Controls
{
    [
    ToolboxData("<{0}:ScriptProcessor runat=server />"),
    ParseChildren(false),
    PersistChildren(false),
    PersistenceMode(PersistenceMode.Attribute)
    ]
    public class ScriptProcessor
        : Control, IScriptProcessor
    {
        [
        Browsable(true),
        Bindable(false),
        Category("Behavior")
        ]
        public string ScriptFolder { get; set; }

        /// <summary>
        /// Get a reference to the current state of the reference manager.
        /// </summary>
        [
        Browsable(false),
        Bindable(false)
        ]
        public List<Resource.Content> Resources { get { return Page.ResourceIncludes().ResourceList(Resource.ContentTypes.javascript); } }
        /// <summary>
        /// Get a reference to the resource manager
        /// </summary>
        [
        Browsable(false),
        Bindable(false)
        ]
        public Resource Manager { get { return Module.Manager; } }

        public ScriptProcessor()
            : base()
        {
            EnableViewState = false;
            EnableTheming = false;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //if (ScriptFolder.IsNullOrEmpty())
            //{
            //    ScriptFolder = JQNConfig.ScriptFolder;
            //}
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Indent++;

            foreach (Resource.Content script in Resources.OrderBy(x => x.uri))
            {
                writer.WriteLine();
                writer.AddAttribute(HtmlTextWriterAttribute.Type, Resource.ContentTypes.javascript);
                writer.AddAttribute(HtmlTextWriterAttribute.Src, String.Format("/JQuery.Net/Script/{0}", script.id));
                writer.RenderBeginTag(HtmlTextWriterTag.Script);
                writer.RenderEndTag();
            }

            writer.Indent--;
        }
    }
}
